package com.csglxt.utils;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.csglxt.domain.Market;
import com.csglxt.domain.User;

/**
 * 
 * @ClassName: Installer
 * @Description: 初始化数据
 * @author Delver_Si
 * @date 2015-5-28 下午11:57:02
 * 
 */
@Component
public class Installer {

    @Resource
    private SessionFactory sessionFactory;

    /**
     * 执行安装
     */
    @Transactional
    public void install() {
        Session session = sessionFactory.getCurrentSession();

        // ==============================================================
        // 保存管理员用户
        User user = new User();
        user.setUsername("admin");
        user.setName("管理员");
        user.setRole("1");
        user.setPassword(DigestUtils.md5Hex("admin"));
        session.save(user); // 保存

        // 初定超市只有一个
        Market market = new Market("超市1", "武汉");
        session.save(market);

    }

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Installer installer = (Installer) ac.getBean("installer");
        installer.install();
    }
}
